<?php
class Profiler{
	var $start_time_s = null;
	var $start_time_u = null;
	var $end_time_s = null;
	var $end_time_u = null;
	
	public function start(){
		list($this->start_time_u, $this->start_time_s) = explode(' ',microtime());
	}
	
	public function end(){
		list($this->end_time_u, $this->end_time_s) = explode(' ',microtime());
		return $this->calculate();
	}
	
	private function calculate(){
		if($this->start_time_s != $this->end_time_s){
			$value = $this->end_time_s - $this->start_time_s;
			$unit = 'seconds';
		}elseif($this->end_time_u - $this->start_time_u >= 0.001){
			$value = ($this->end_time_u - $this->start_time_u) * 1000;
			$unit = 'miliseconds';
		}else{
			$value = ($this->end_time_u - $this->start_time_u) * 1000000;
			$unit = 'microseconds';
		}
		return $value.' '.$unit;
	}
}
?>